package base.Until;

import base.pojo.menu.Menu;
import net.sf.json.JSONObject;


/**
 * 自定义菜单工具类
 * 
 * @author liufeng
 * @date 2013-10-17
 */
public class MenuUtil {

	// 菜单创建（POST）
	public final static String menu_create_url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=ACCESS_TOKEN";
	// 菜单查询（GET）
	public final static String menu_get_url = "https://api.weixin.qq.com/cgi-bin/menu/get?access_token=ACCESS_TOKEN";
	// 菜单删除（GET）
	public final static String menu_delete_url = "https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=ACCESS_TOKEN";
	//获取素材列表
	public final static String query_sclb_url = "https://api.weixin.qq.com/cgi-bin/material/batchget_material?access_token=ACCESS_TOKEN";

	//获得文章内容
	public final static String query_wznr_url = "https://api.weixin.qq.com/cgi-bin/material/get_material?access_token=ACCESS_TOKEN&media_id=media_id";

	/**
	 * 创建菜单
	 * 
	 * @param menu 菜单实例
	 * @param accessToken 凭证
	 * @return true成功 false失败
	 */
	public static boolean createMenu(Menu menu, String accessToken) {
		boolean result = false;
		String url = menu_create_url.replace("ACCESS_TOKEN", accessToken);
		// 将菜单对象转换成json字符串
		String jsonMenu = JSONObject.fromObject(menu).toString();
		// 发起POST请求创建菜单
		JSONObject jsonObject = CommonUtil.httpsRequest(url, "POST", jsonMenu);

		if (null != jsonObject) {
			int errorCode = jsonObject.getInt("errcode");
			String errorMsg = jsonObject.getString("errmsg");
			if (0 == errorCode) {
				result = true;
			} else {
				result = false;
			}
		}

		return result;
	}
	
	/**
	 * 获得素材列表
	 * @param menu
	 * @param accessToken
	 * @return
	 */
	public static String Query_Cclb(String xlm, String accessToken) {
		String result = "";
		String url = query_sclb_url.replace("ACCESS_TOKEN", accessToken);
		// 将菜单对象转换成json字符串
		String jsonMenu = JSONObject.fromObject(xlm).toString();
		// 发起POST请求创建菜单
		JSONObject jsonObject = CommonUtil.httpsRequest(url, "POST", jsonMenu);

		if (null != jsonObject) {
			result = jsonObject.toString();
		}

		return result;
	}
	
	/**
	 * 查询某一文章内容
	 * @param xlm
	 * @param accessToken
	 * @return
	 */
	
	public static String Query_wznr(String xlm, String accessToken) {
		String result = "";
		String url = query_wznr_url.replace("ACCESS_TOKEN", accessToken);
	//url = query_wznr_url.replace("media_id", "fLL7LiQ4uMiT167GcQCsM_LamPemJZSj5YHpoNC_tVE");
		
		String xlml ="{\"media_id\":\"fLL7LiQ4uMiT167GcQCsM_LamPemJZSj5YHpoNC_tVE\"}";
		// 将菜单对象转换成json字符串
		String jsonMenu = JSONObject.fromObject(xlml).toString();
		// 发起POST请求创建菜单
		JSONObject jsonObject = CommonUtil.httpsRequest(url, "POST", jsonMenu);

		if (null != jsonObject) {
			result = jsonObject.toString();
		}

		return result;
	}

	/**
	 * 查询菜单
	 * 
	 * @param accessToken 凭证
	 * @return
	 */
	public static String getMenu(String accessToken) {
		String result = null;
		String requestUrl = menu_get_url.replace("ACCESS_TOKEN", accessToken);
		// 发起GET请求查询菜单
		JSONObject jsonObject = CommonUtil.httpsRequest(requestUrl, "GET", null);

		if (null != jsonObject) {
			result = jsonObject.toString();
		}
		return result;
	}

	/**
	 * 删除菜单
	 * 
	 * @param accessToken 凭证
	 * @return true成功 false失败
	 */
	public static boolean deleteMenu(String accessToken) {
		boolean result = false;
		String requestUrl = menu_delete_url.replace("ACCESS_TOKEN", accessToken);
		// 发起GET请求删除菜单
		JSONObject jsonObject = CommonUtil.httpsRequest(requestUrl, "GET", null);

		if (null != jsonObject) {
			int errorCode = jsonObject.getInt("errcode");
			String errorMsg = jsonObject.getString("errmsg");
			if (0 == errorCode) {
				result = true;
			} else {
				result = false;
			}
		}
		return result;
	}
}